import os

import allure
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from common.Path_API import ERROR_DIR
from selenium.webdriver.remote.webdriver import WebDriver
from common.Logging_API import log1
import time
"""
BasePage: 封装的是一个所有页面的父类，在父类张红封装一些页面常用的操作方法
    1、查找元素
    2、点击元素
    3、输入内容
    4、清空输入框
    5、获取元素文本
    6、获取元素的属性
    7、等待元素可见
    8、等待元素可点击
    9、等待元素存在
    10、iframe切换
    11、错误截图
    
    
    12、窗口切换
    13、打开新窗口
    
    14、执行js代码
    
    
"""


class BasePage:
    def __init__(self, driver: WebDriver):
        self.driver = driver

    # 寻找元素
    def get_element(self, loc, desc):
        """

        :param loc: 输入元素位置
        :param desc: 输入描述
        :return:
        """
        try:
            self.driver.find_element(*loc)
        except Exception as e:
            log1.error("用例--【{}】--失败".format(desc))
            log1.exception(e)
            self.error_save_img(desc)
            raise e
        else:
            log1.info("用例--【{}】--成功".format(desc))


    # 单机
    def get_click(self, loc, desc):
        """

        :param loc:
        :param desc:
        :return:
        """

        try:
            self.driver.find_element(*loc).click()
        except Exception as e:
            log1.error("点击元素--【{}】--失败".format(desc))
            log1.exception(e)
            self.error_save_img(desc)
            raise e
        else:
            log1.info("点击元素--【{}】--成功".format(desc))

    # 输入数据
    def input_text(self, loc, value, desc):
        """

        :param loc:
        :param value:   传入需要输入的值
        :param desc:
        :return:
        """
        try:
            self.driver.find_element(*loc).send_keys(value)
        except Exception as e:
            log1.error("输入元素--【{}】--失败".format(desc))
            log1.exception(e)
            self.error_save_img(desc)
            raise e
        else:
            log1.info("输入元素--【{}】--成功".format(desc))

    # 清空数据
    def clear_text(self, loc, desc):
        """

        :param loc:
        :param desc:
        :return:
        """
        try:
            self.driver.find_element(*loc).clear()
        except Exception as e:
            log1.error("清空元素--【{}】--失败".format(desc))
            log1.exception(e)
            self.error_save_img(desc)
            raise e
        else:
            log1.info("清空元素--【{}】--成功".format(desc))

    # 5、获取元素文本
    def get_element_text(self, loc, desc):
        """

        :param loc:
        :param desc:
        :return:
        """
        try:
            res = self.driver.find_element(*loc).text
        except Exception as e:
            log1.error("用例--【{}】--失败".format(desc))
            log1.exception(e)
            self.error_save_img(desc)
            raise e
        else:
            log1.info("用例--【{}】--成功".format(desc))
        return res

    # 6、获取元素的属性
    def get_element_attr(self, loc, attr, desc):
        """

        :param loc:
        :param desc:
        :return:
        """
        try:
            res = self.driver.find_element(*loc).get_attribute(attr)
        except Exception as e:
            log1.error("获取元素--【{}】--的属性失败".format(desc))
            log1.exception(e)
            self.error_save_img(desc)
            raise e
        else:
            log1.info("获取元素--【{}】--的属性成功".format(desc))
        return res

    # 7、等待元素可见
    def wait_element_see(self, loc, desc):
        try:
            ele = WebDriverWait(self.driver, 10, 0.5).until(
                expected_conditions.visibility_of_element_located(loc)
            )

        except Exception as e:
            log1.error("等待元素可见--【{}】--失败".format(desc))
            log1.exception(e)
            self.error_save_img(desc)
            raise e
        else:
            log1.info("等待元素可见--【{}】--成功".format(desc))
        return ele

    # 8、等待元素可点击
    def wait_element_click(self, loc, desc):
        try:
            ele = WebDriverWait(self.driver, 10, 0.5).until(
                expected_conditions.element_to_be_clickable(loc)
            )

        except Exception as e:
            log1.error("等待元素可点击--【{}】--失败".format(desc))
            log1.exception(e)
            self.error_save_img(desc)
            raise e
        else:
            log1.info("等待元素可点击--【{}】--成功".format(desc))
        return ele

    # 9、等待元素存在
    def wait_element_located(self, loc, desc):
        try:
            ele = WebDriverWait(self.driver, 10, 0.5).until(
                expected_conditions.presence_of_element_located(loc)
            )

        except Exception as e:
            log1.error("等待元素存在--【{}】--失败".format(desc))
            log1.exception(e)
            self.error_save_img(desc)
            raise e
        else:
            log1.info("等待元素存在--【{}】--成功".format(desc))
        return ele

    # 10、iframe切换
    def iframe_switch_to(self, loc, desc):
        try:
            ele = WebDriverWait(self.driver, 10, 0.5).until(
                expected_conditions.frame_to_be_available_and_switch_to_it(loc)
            )

        except Exception as e:
            log1.error("等待iframe切换--【{}】--失败".format(desc))
            log1.exception(e)
            self.error_save_img(desc)
            raise e
        else:
            log1.info("等待元素iframe切换--【{}】--成功".format(desc))
        return ele

    # 11、错误截图
    def error_save_img(self, desc):
        i_date = time.strftime("%Y-%m-%d_%H_%M_%S_")
        filename = i_date + desc + '.png'
        file_path = os.path.join(ERROR_DIR, filename)
        try:
            self.driver.save_screenshot(file_path)
        except Exception as e:
            log1.error("对-【{}】--操作进行截图--失败".format(desc))
            log1.exception(e)
            raise e
        else:
            with open(file_path, "失败截图") as f:
                content = f.read()
            allure.attach(content, allure.attachment_type.PNG)
            log1.info("对-【{}】--操作进行截图--成功".format(desc))
            log1.info("图片名为{}".format(file_path))
